TGGS Computer Science

Spring 1 - Lesson 5 - Pre-Assessment Consolidation Lesson

🌟 Learning Objectives

🌟 We are learning to consolidate our understanding of Python


✅ Outcomes

  • We will consolidate our understanding of inputs, outputs and variables
  • We will consolidate our understanding of Selection IF and ELIF
  • We will consolidate our understanding of While loops and For Loops
  • We will consolidate our understanding of Lists (1D and 2D)
🐍 Activity 1 – Inputs, Outputs and Variables

🐍 Activity 1.1 - Assign an input to a variable and output it

  • Make a variable called sport
  • Assign a user input to the variable
  • The prompt should ask the user to input their favourite sport
  • Output a message telling them that you also like the sport
Click to see how this should look
sport = input("Input your favourite sport: ")
print(f"Wow, I really like {sport} as well!")

🐍 Activity 1.2 Casting an input to an integer and multiplying it

  • Make a variable called num
  • Create an input
  • The prompt should ask the user to input a number they wish to multiply by 2
  • Cast the input to an integer
  • Output the number multiplied by 2
Click to see how this should look
num = int(input("Enter a number you wish to multiply by 2: "))
print(num * 2)
🐍 Activity 2 – Selection and Branching Selection

🐍 Activity 2.1 - Working with selection (IF / ELSE)

  • Make a variable called capital
  • Assign an input to the variable
  • The prompt of the input should ask the user to input the capital of Poland
  • Use a selection sequence (IF / ELSE) to decide if they have guessed correctly
  • When testing capital against "warsaw" use the .lower() function
  • Output either correct or incorrect depending on the user's answer
Click to see how this should look
capital = input("Name the capital of Poland: ")
if capital.lower() == "warsaw":
    print("Correct!")
else:
    print("Incorrect")

🐍 Activity 2.2 - Working with selection (IF / ELIF / ELSE)

  • Make a variable called colour
  • Assign a user input to the variable
  • The prompt of the input should ask the user to input a primary colour
  • Use a branching selection sequence (IF / ELIF / ELSE) to decide if they have guessed correctly
  • When testing colour against "red", "blue" or "yellow" use the .lower() function
  • Output either correct or incorrect depending on the user's answer
Click to see how this should look
colour = input("Name a primary colour: ")
if colour.lower() == "red":
    print("Correct!")
elif colour.lower() == "blue":
    print("Correct!")
elif colour.lower() == "yellow":
    print("Correct!")
else:
    print("Incorrect")
🐍 Activity 3 – While loops

🐍 Activity 3.1 - Working with a conditional while loop

  • Make a variable called password
  • Assign a user input to the variable
  • The prompt of the input should ask the user to input the password
  • Use a while statement to test if the password is not equal to "Secret123"

  • The code (indented) within the while loop will be as follows
    • Output that the password is incorrect and the user must try again
    • Provide the user with an input
    • The input will be assigned to the variable password

  • A few lines after the for loop (indented back to the left) output "Password accepted"
Click to see how this should look
password = input("Enter the password: ")

while password != "Secret123":
    print("Password is incorrect, please try again: ")
    password = input("Enter the password: ")
    
print("Password accepted")

🐍 Activity 3.2 - Working with a while True loop

  • Start off with a while True loop

  • The code within the loop will go as follows:
    • A variable called guess is created
    • A user input will be assigned to the variable
    • The prompt for the input will ask the user to input a primary colour
    • A branching selection (IF / ELIF / ELSE) sequence will be used to see if the user has entered "red", "yellow" or "blue"
    • For each correct answer the program will output "Correct"
    • On the line below "Correct" there will be a break command
    • For the else command, the program will output "Incorrect" but there will not be a break command
Click to see how this should look
while True:
    guess = input("Input a primary colour: ")
    
    if guess.lower() == "red":
        print("Correct")
        break
    elif guess.lower() == "yellow":
        print("Correct")
        break
    elif guess.lower() == "blue":
        print("Correct")
        break
    else:
        print("Incorrect")
🐍 Activity 4 – For Loops and Lists

🐍 Activity 4.1 - For loop to output the 8 times table

  • Create a for loop with i in range of 1 to 13
  • In each iteration, output the value of i multiplied by 8
  • Ensure there is an appropriate message along with it e.g.
  • To do this, you may wish to use an f String
  • 1 X 8 = 8
    2 X 8 = 16
    3 X 8 = 24
    
    etc....
    
  • To do this, you may wish to use an f String

Click to see how this should look
for i in range(1, 13):
    print(f"{i} X 8 = {i * 8}")

🐍 Activity 4.2 - For loop through a 1 dimensional list

  • You will have been given a list of Olympic Sports
  • sports = ["Shotput", "Javelin", "Triple Jump", "High Jump", "Discus", "Diving"]
    
  • Create a for loop with a range that will ensure each list item gets output
  • In each iteration, use i to output each list item
  • Ensure there is an appropriate message along with it e.g.
  • To do this, you may wish to use an f String
  • Shotput will feature in the 2028 Olympics
    Javelin will feature in the 2028 Olympics
    Triple Jump will feature in the 2028 Olympics
    
    etc....
    

Click to see how this should look

#index       0           1            2             3           4        5
sports = ["Shotput", "Javelin", "Triple Jump", "High Jump", "Discus", "Diving"]

for i in range(0, 6):
    print(f"{sports[i]} will feature in the 2028 Olympics")

🐍 Activity 5 – Outputting data from 2 Dimensional Lists

🐍 Activity 5.1 - Output both items in each record

  • You have been given a 2 dimensional list comprised of the following records
  • landmarks = [
        ["Eiffel Tower", "Paris"],
        ["Taj Mahal", "Agra"],
        ["Forbidden Palace", "Beijing"],
        ["Golden Gate Bridge", "San Francisco"],
        ["Royal Mile", "Edinburgh"],
        ["Burj Khalifa", "Dubai"],
        ["Hagia Sophia", "Istanbul"],
        ["Sagrada Familia", "Barcelona"]
        ]
    
  • Write a for loop, where the range will successfully navigate all records
  • During each iteration, output the first and second item of whichever record matches the loop counter variable (i)
  • Each output should be put together in an appropriate sentence
  • The Eiffel Tower is in Paris
    The Taj Mahal is in Agra
    The Forbidden Palace is in Beijing
    
    etc....
    

Click to see how this should look

landmarks = [
    ["Eiffel Tower", "Paris"],
    ["Taj Mahal", "Agra"],
    ["Forbidden Palace", "Beijing"],
    ["Golden Gate Bridge", "San Francisco"],
    ["Royal Mile", "Edinburgh"],
    ["Burj Khalifa", "Dubai"],
    ["Hagia Sophia", "Istanbul"],
    ["Sagrada Familia", "Barcelona"]
    ]

for i in range(0, 8):
    print(f"The {landmarks[i][0]} is in {landmarks[i][1]}")
    

🐍 Activity 5.2 - Searching a 2 Dimensional List

  • You have been given a 2 dimensional list comprised of the following records
  • Each is the name of an athlete and the medal they won
  • medals = [
        ["Keely Hodgkinson", "gold"],       
        ["Adam Peaty", "gold"],             
        ["Duncan Scott", "silver"],         
        ["Ben Proud", "silver"],           
        ["Tom Daley", "bronze"],            
        ["Bradley Wiggins", "bronze"],      
        ["Jessica Ennis-Hill", "gold"],    
        ["Christine Ohuruogu", "bronze"] 
    ]
    
  • Create a variable called search
  • Assign a user input to the variable
  • The prompt for the user input will ask the user to input a type of medal "bronze", "silver" or "gold"
  • Write a for loop, where the range will successfully navigate all records
  • During each iteration, use an IF statement to test if the medal in a particular record matches the user search
  • If a match is found, output the name of the athlete and the medal in a meaningful sentence
  • Input a type of medal (bronze, silver or gold): bronze
    Tom Daley won bronze
    Bradley Wiggins won bronze
    Christine Ohuruogu won bronze
    
    etc....
    

Click to see how this should look

medals = [
    ["Keely Hodgkinson", "gold"],       
    ["Adam Peaty", "gold"],     
    ["Duncan Scott", "silver"],         
    ["Ben Proud", "silver"],            
    ["Tom Daley", "bronze"],           
    ["Bradley Wiggins", "bronze"],      
    ["Jessica Ennis-Hill", "gold"],     
    ["Christine Ohuruogu", "bronze"]
]

search = input("Input a type of medal (bronze, silver or gold): ")

for i in range(0, 8):
    if medals[i][1] == search.lower():
    print(f"The {medals[i][0]} won {medals[i][1]}")